import enum
import json


# 断言类型
class AssertType(enum.Enum):
    eql = "eql"
    contains = "contains"

    @staticmethod
    def assert_eql(real, except_val):
        assert real == except_val, "实际值 %s 不等于期望值 %s" % (real, except_val,)

    @staticmethod
    def assert_contains(real, except_val):
        assert str(except_val) in json.dumps(real), f"实际值 %s 中未包含期望值 %s" % (real, except_val)

    @classmethod
    def assert_result(cls, assert_type, real, except_val):
        for item in cls:
            if item.value == assert_type:
                return getattr(cls, "assert_%s" % assert_type)(real, except_val)
        else:
            raise AttributeError("无效的断言类型 %s" % assert_type)


# 请求头类型
class ContentTypeEnu(enum.Enum):
    json_type = "application/json;charset=UTF-8"
    form_type = "application/x-www-form-urlencoded"

    @staticmethod
    def json_type_data(data):
        return json.dumps(data)

    @staticmethod
    def form_type_data(data):
        return data

    @classmethod
    def get_content_data(cls, content_type, data):
        for item in cls:
            if content_type == item.value:
                return getattr(cls, "%s_data" % item.name)(data)
        else:
            raise AttributeError("无效的 Content-Type: %s" % content_type)
